/*
 * Copyright (c) 2013 - 2015, Freescale Semiconductor, Inc.
 * Copyright 2016-2017 NXP
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <stdio.h>

#include "fsl_common.h"
#include "board.h"

#include "i2c_hal.h"


/*******************************************************************************
 * Definitions
 ******************************************************************************/


/*******************************************************************************
 * Prototypes
 ******************************************************************************/
void lm75_init(void);
uint16_t lm75_read(void);

/*******************************************************************************
 * Code
 ******************************************************************************/
/*!
 * @brief Main function
 */
int main(void)
{
    board_init();
    printf("i2c master basic.\r\n");

    lm75_init();

    while (1)
    {
        getchar();
        printf("lm75_read(): 0x%-2x\r\n", lm75_read() );
    }
}

#define LM75_I2C_SLAVE_ADDRESS 0x4c /* 7-bit address. 100_1100. */
//#define LM75_I2C_SLAVE_ADDRESS 0x45 /* 7-bit address. 100_1100. */
#define LM75_I2C   I2C0

void lm75_init(void)
{
    /* pins are already setup in BOARD_InitPins() by config tool. */

    /* i2c controller.1 */
    i2c_conf_master(LM75_I2C, eI2C_Speed_400k);
}

uint16_t lm75_read(void)
{
    uint8_t buff[3];
    uint16_t dat;

    buff[0] = 0u;
    i2c_master_write(LM75_I2C, LM75_I2C_SLAVE_ADDRESS, buff, 1);
    i2c_master_read(LM75_I2C, LM75_I2C_SLAVE_ADDRESS, buff, 2);

    dat = buff[0];
    dat <<= 8u;
    dat |= buff[1];

    return dat;
}

/* EOF. */

